import { isLightDevice } from './pidUtils'
import { getRemoteDataList } from '../utils/deviceUtils';
import { meshLog,SPRemove,SPGet,SPSet } from '../config';
//保存localStorage上的area的修改信息的
export const saveAreaInfor = (areaItem) => {
    //meshLog("修改参数:", areaItem);
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    for (let i = 0; i < areaSetx.length; i++) {
        if (areaSetx[i].areaNo == areaItem.areaNo) {
            areaSetx[i].dim = areaItem.dim;
            areaSetx[i].tem = areaItem.tem;
            areaSetx[i].rgbH = areaItem.rgbH;
            areaSetx[i].rgbS = areaItem.rgbS;
            areaSetx[i].rgbL = areaItem.rgbL;
            areaSetx[i].isrgb = areaItem.isrgb;
            areaSetx[i].isOnoff = areaItem.isOnoff;
            areaSetx[i].isOnline = areaItem.isOnline;
            areaSetx[i].isdimdeng = areaItem.isdimdeng;
        }
    }
    SPRemove('areaSetx');//删除loaclstorage上的areaSetx
    SPSet('areaSetx', JSON.stringify(areaSetx))
    // meshLog("areaSetx:", areaSetx);
}
//保存localStorage上组中所有灯的修改信息的
export const saveLightInfor = (devSetItem) => {
    //meshLog("修改参数:", devSetItem);
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    for (let i = 0; i < deviceSetx.length; i++) {
        for (let index = 0; index < devSetItem.length; index++) {
            if (deviceSetx[i].address == devSetItem[index].address) {
                deviceSetx[i].dim = devSetItem[index].dim;
                deviceSetx[i].tem = devSetItem[index].tem;
                deviceSetx[i].rgbH = devSetItem[index].rgbH;
                deviceSetx[i].rgbS = devSetItem[index].rgbS;
                deviceSetx[i].rgbL = devSetItem[index].rgbL;
                deviceSetx[i].isrgb = devSetItem[index].isrgb;
                deviceSetx[i].isOnoff = devSetItem[index].isOnoff;
                deviceSetx[i].isOnline = devSetItem[index].isOnline;
                deviceSetx[i].isdimdeng = devSetItem[index].isdimdeng;
            }
        }
    }
    SPSet('deviceSetx', JSON.stringify(deviceSetx))
    // meshLog("deviceSetx:", deviceSetx);
}
//保存localStorage上group的修改信息的
export const saveGroupSetInfor = (groupSetItem) => {
    // meshLog("groupSetItem修改参数:", groupSetItem);
    let groupSetx = JSON.parse(SPGet("groupSetx"));
    for (let i = 0; i < groupSetx.length; i++) {
        for (let index = 0; index < groupSetItem.length; index++) {
            if (groupSetx[i].groupNo == groupSetItem[index].groupNo) {
                //meshLog(groupSetx[i]);
                groupSetx[i].dim = groupSetItem[index].dim;
                groupSetx[i].tem = groupSetItem[index].tem;
                groupSetx[i].rgbH = groupSetItem[index].rgbH;
                groupSetx[i].rgbS = groupSetItem[index].rgbS;
                groupSetx[i].rgbL = groupSetItem[index].rgbL;
                groupSetx[i].isrgb = groupSetItem[index].isrgb;
                groupSetx[i].isOnoff = groupSetItem[index].isOnoff;
                groupSetx[i].isOnline = groupSetItem[index].isOnline;
                groupSetx[i].isdimdeng = groupSetItem[index].isdimdeng;
            }
        }
    }
    SPSet('groupSetx', JSON.stringify(groupSetx))
    //meshLog("groupSetxgroupSetxgroupSetx:", groupSetx);
}
//修改状态中所有灯的的信息 有返回值,主要是针对开关灯的状态更新//仅修改在线的灯
export const saveStateAllLightInfor = (areasetDevs, value) => {
    //meshLog("修改参数:", areasetDevs);
    for (let i = 0; i < areasetDevs.length; i++) {
        if (areasetDevs[i].isOnline === '01') {
            areasetDevs[i].isOnoff = value;
        }
    }
    //meshLog("areasetDevs:", areasetDevs);
    return areasetDevs
}
//修改状态中单灯灯的信息 有返回值 主要是针对开关灯的状态更新
export const saveStateOneLightInfor = (areasetDevs, value, addr) => {
    //meshLog("修改参数:", areasetDevs);
    for (let i = 0; i < areasetDevs.length; i++) {
        if (areasetDevs[i].address === addr) {
            areasetDevs[i].isOnoff = value;
        }
    }
    //meshLog("areasetDevs:", areasetDevs);
    return areasetDevs
}
//修改状态中单个subgroup的信息 有返回值 主要是针对开关灯的状态更新
export const saveStateSubgrouInfor = (groupSetItem, value, addr) => {
    //meshLog("修改参数:", areasetDevs);
    for (let i = 0; i < groupSetItem.length; i++) {
        if (groupSetItem[i].address === addr) {
            groupSetItem[i].isOnoff = value;
        }
    }
    //meshLog("areasetDevs:", areasetDevs);
    return groupSetItem
}
//修改状态中所有的组的的信息 有返回值 主要是针对开关灯的状态更新
export const saveStateAreaSetInfor = (areaSet, value, areaNo) => {
    for (let i = 0; i < areaSet.length; i++) {
        if (areaSet[i].areaNo === areaNo) {
            areaSet[i].isOnoff = value;
        }
    }
    //meshLog("areasetDevs:", areasetDevs);
    return areaSet
}
//修改状态中所有的组的的信息 有返回值 主要是针对Dim的状态更新
export const saveStateAreaDimInfor = (value, areaNo) => {
    let areaSet = JSON.parse(SPGet("areaSetx"))
    for (let i = 0; i < areaSet.length; i++) {
        if (areaSet[i].areaNo === areaNo) {
            areaSet[i].dim = value;
        }
    }
    //meshLog("areaSet:", areaSet);
    SPRemove('areaSetx');
    SPSet('areaSetx', JSON.stringify(areaSet))//存到localStorage
    return areaSet
}
//修改状态中所有的组的的信息 有返回值 主要是针对tem的状态更新
export const saveStateAreaTemInfor = (value, areaNo) => {
    let areaSet = JSON.parse(SPGet("areaSetx"))
    // meshLog('homeUtils.jsx,129行,areaNo', areaNo);
    for (let i = 0; i < areaSet.length; i++) {
        if (areaSet[i].areaNo === areaNo) {
            areaSet[i].tem = value;
        }
    }
    // meshLog("areasetDevs:", areaSet);
    SPRemove('areaSetx');
    SPSet('areaSetx', JSON.stringify(areaSet))//存到localStorage
    return areaSet
}
//修改状态中所有的组的的信息 有返回值 主要是针对rgbH的状态更新
export const saveStateAreargbHInfor = (value, areaNo) => {
    let areaSet = JSON.parse(SPGet("areaSetx"))
    //meshLog('homeUtils.jsx,129行,areaNo', areaNo);
    for (let i = 0; i < areaSet.length; i++) {
        if (areaSet[i].areaNo === areaNo) {
            areaSet[i].rgbH = value;
        }
    }
    //meshLog("saveStateAreargbHInfor:", areaSet);
    SPRemove('areaSetx');
    SPSet('areaSetx', JSON.stringify(areaSet))//存到localStorage
    return areaSet
}
//查找属于这个组的所有的light和subgroup的灯 有返回灯的信息
export const searchAllLightOfThisArea = (areaNo) => {
    //meshLog("修改参数:", areasetDevs);
    let deviceSetxx = JSON.parse(SPGet("deviceSetx"));
    let areadevice = deviceSetxx.filter((item) => {
        const pamdun2 = item.areaNo;
        if (pamdun2 === areaNo) {
            if (isLightDevice(item.pid)) {
                return item
            }
        }
    })
    //meshLog("areasetDevs:", areasetDevs);
    return areadevice
}
//查找属于这个组的所有的light，不包括subgroup 有返回值
export const searchLightOfThisArea = (areaNo) => {
    //meshLog("修改参数:", areasetDevs);
    let deviceSetxx = JSON.parse(SPGet("deviceSetx"));
    let areasetDevs = deviceSetxx.filter((item) => {
        const paa = '000';
        const pamdun1 = item.groupNo.substring(0, 3);
        const pamdun2 = item.areaNo;
        const pidd = item.pid
        if (pamdun2 === areaNo && pamdun1 === paa && (isLightDevice(pidd))) {
            return item
        }
    })
    //meshLog("areasetDevs:", areasetDevs);
    return areasetDevs
}
//查找属于这个组的所有的subgroup 有返回值
export const searchSubgroupOfThisArea = (areaNo) => {
    //meshLog("修改参数:", areasetDevs);
    let grouSetx = JSON.parse(SPGet("groupSetx"))
    let groupSet = grouSetx.filter((item) => {
        const pamdun2 = item.areaNo;
        if (pamdun2 === areaNo) {
            return item
        }
    })
    //meshLog("areasetDevs:", areasetDevs);
    return groupSet
}
//查询属于这个subgroup的灯的信息,并且返回灯的信息
export const getSubGroupLight = (groupNo) => {
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    let groupNopuan = groupNo
    let subdevice = deviceSetx.filter((item) => {
        const pamdun1 = item.groupNo;
        const pidd = item.pid;
        if (pamdun1 === groupNopuan && isLightDevice(pidd)) {
            return item
        }
    })
    return subdevice
}
//通过单灯的信息来修改area的信息并且保存localStorage上的area的修改信息的
export const lightToSaveAreaInfor = (devItem) => {
    // meshLog("修改参数:", devItem);
    let areaSetx = JSON.parse(SPGet("areaSetx"));
    for (let i = 0; i < areaSetx.length; i++) {
        if (areaSetx[i].areaNo === devItem.areaNo) {
            areaSetx[i].dim = devItem.dim;
            areaSetx[i].tem = devItem.tem;
            // areaSetx[i].rgbH = devItem.rgbH;
            // areaSetx[i].rgbS = devItem.rgbS;
            // areaSetx[i].rgbL = devItem.rgbL;
            // areaSetx[i].isrgb = devItem.isrgb;
        }
    }
    SPSet('areaSetx', JSON.stringify(areaSetx))
    // meshLog("areaSetx:", areaSetx);
}
//判断是否包含遥控器
export const judgeOccuRemote = (areaNo) => {
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    //获取遥控器数据
    let arrayR = getRemoteDataList(deviceSetx, areaNo);
    if (arrayR.length > 0) {
        return true;
    }
    return false;
}
//判断是否启用Smartshift
export const judgeOccuSmart = (areaNo) => {
    if (typeof (areaNo) == 'undefined' || areaNo.length != 4 || areaNo.substring(3, 4) != '0') {
        return false;
    }
    if (parseInt(areaNo, 16) < parseInt('C010', 16)) {
        return false;  //过滤未分组
    }
    let smartshiftSet = JSON.parse(SPGet("smartshiftSetx"));
    let areaSet = smartshiftSet.areaSet;
    if (areaSet.length == 0 || smartshiftSet.smartType == 0) {
        return false;  //过滤未启用SmartShift
    }
    for (let i = 0; i < areaSet.length; i++) {
        if (areaSet[i].action == 0) {
            continue;
        }
        if (areaSet[i].address == parseInt(areaNo, 16) && areaSet[i].action > 0) {
            return true; //当前area启用smartshift
        }
    }
    return false;
}
//查询属于这个subgroup的灯的信息,并且返回
export const getSubGroupLightNums = (groupNo) => {
    let deviceSetx = JSON.parse(SPGet("deviceSetx"));
    let groupNopuan = groupNo
    let subdevice = deviceSetx.filter((item) => {
        const pamdun1 = item.groupNo;
        const pidd = item.pid;
        if (pamdun1 === groupNopuan && isLightDevice(pidd)) {
            return item
        }
    })
    if (subdevice.length == 0) {
        return true
    } else {
        return false
    }
}
//查询组，单灯，subgroup 的开关灯信息，并且有返回值
export const checkLightOnOffInfor = (light, number) => {
    let check = false
    let areaSetitem = ''
    if (number == 1) {
        let areaSet = JSON.parse(SPGet("areaSetx"))
        areaSetitem = areaSet.filter((item) => {
            if (item.areaNo == light) {
                return item
            }
        })
    } else if (number == 2) {
        let deviceSetx = JSON.parse(SPGet("deviceSetx"));
        areaSetitem = deviceSetx.filter((item) => {
            if (item.address == light) {
                return item
            }
        })
        //meshLog('是灯');
    } else if (number == 3) {
        let grouSetx = JSON.parse(SPGet("groupSetx"))
        areaSetitem = grouSetx.filter((item) => {
            if (item.groupNo == light) {
                return item
            }
        })
        //meshLog('是subgroup');
    }
    meshLog('homeUtils.jsx,129行,点击的详细信息areaSetitem', areaSetitem);
    if (areaSetitem[0].isOnoff == '00') {
        check = true
    } else {
        check = false
    }
    return check
}
